<?php

namespace app\admin\model\user;

use app\admin\model\order\StoreOrder;
use app\admin\model\system\SystemUserLevel;
use basic\BaseModel;
use traits\ModelTrait;

class User extends BaseModel
{
    use ModelTrait;

    /**
     * 主键
     * @var string
     */
    protected $pk = 'uid';

    /**
     * 模型名称
     * @var string
     */
    protected $name = "user";

    /**
     * @title 获取会员的数量
     */
    public static function getCount()
    {
        return self::count();
    }

    /**
     * @title 获取会员的数据列表
     * @param $where array 条件
     */
    public static function getUserList($where)
    {
        $model = self::setWherePage(self::setWhere($where), $where, ['w.sex', 'w.province', 'w.city', 'u.status', 'u.is_promoter'], ['u.nickname', 'u.uid', 'u.phone']);
        $list = $model->alias('u')
            ->join('WechatUser w', 'u.uid=w.uid')
            ->field('u.*,w.country,w.province,w.city,w.sex,w.unionid,w.openid,w.routine_openid,w.groupid,w.tagid_list,w.subscribe,w.subscribe_time')
            ->page((int)$where['page'], (int)$where['limit'])
            ->select()
            ->each(function ($item) {
                $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
                if ($item['last_time']) $item['last_time'] = date('Y-m-d H:i:s', $item['last_time']);//最近一次访问日期
                else $item['last_time'] = '无访问';//最近一次访问日期
                self::edit(['pay_count' => StoreOrder::getUserCountPay($item['uid'])], $item['uid']);
                $item['extract_count_price'] = UserExtract::getUserCountPrice($item['uid']);//累计提现
                if ($item['spread_uid']) {
                    $item['spread_uid_nickname'] = self::where('uid', $item['spread_uid'])->value('nickname') . '/' . $item['spread_uid'];
                } else {
                    $item['spread_uid_nickname'] = '无';
                }
                if ($item['openid'] != '' && $item['routine_openid'] != '') {
                    $item['user_type'] = '通用';
                } else if ($item['openid'] == '' && $item['routine_openid'] != '') {
                    $item['user_type'] = '小程序';
                } else if ($item['openid'] != '' && $item['routine_openid'] == '') {
                    $item['user_type'] = '公众号';
                } else if ($item['user_type'] == 'h5') {
                    $item['user_type'] = 'H5';
                } else $item['user_type'] = '其他';
                if ($item['sex'] == 1) {
                    $item['sex'] = '男';
                } else if ($item['sex'] == 2) {
                    $item['sex'] = '女';
                } else $item['sex'] = '保密';
                $item['vip_name'] = false;
                $levelinfo = UserLevel::where('uid', $item['uid'])->where('level_id',$item['level'])->where('is_del', 0)->order('grade desc')->field('level_id,is_forever,valid_time')->find();
                if ($levelinfo) {
                    if ($levelinfo['is_forever']) $item['vip_name'] = SystemUserLevel::where('id', $levelinfo['level_id'])->value('name');
                    else if (time() < $levelinfo['valid_time']) $item['vip_name'] = SystemUserLevel::where('id', $levelinfo['level_id'])->value('name');
                }
            });
        $count = self::setWherePage(self::setWhere($where), $where, ['w.sex', 'w.province', 'w.city', 'u.status', 'u.is_promoter'], ['u.nickname', 'u.uid'])->alias('u')->join('WechatUser w', 'u.uid=w.uid')->count();
        return ['count' => $count, 'data' => $list];

    }

    /*
     * 设置搜索条件
     */
    public static function setWhere($where)
    {
        if ($where['order'] != '') {
            $model = self::order(self::setOrder($where['order']));
        } else {
            $model = self::order('u.uid desc');
        }
        if ($where['user_time_type'] == 'visitno' && $where['user_time'] != '') {
            list($startTime, $endTime) = explode(' - ', $where['user_time']);
            $endTime = strtotime($endTime) + 24 * 3600;
            $model = $model->where("u.last_time < " . strtotime($startTime) . " OR u.last_time > " . $endTime);
        }
        if ($where['user_time_type'] == 'visit' && $where['user_time'] != '') {
            list($startTime, $endTime) = explode(' - ', $where['user_time']);
            $model = $model->where('u.last_time', '>', strtotime($startTime));
            $model = $model->where('u.last_time', '<', strtotime($endTime) + 24 * 3600);
        }
        if ($where['user_time_type'] == 'add_time' && $where['user_time'] != '') {
            list($startTime, $endTime) = explode(' - ', $where['user_time']);
            $model = $model->where('u.add_time', '>', strtotime($startTime));
            $model = $model->where('u.add_time', '<', strtotime($endTime) + 24 * 3600);
        }
        if ($where['pay_count'] !== '') {
            if ($where['pay_count'] == '-1') $model = $model->where('pay_count', 0);
            else $model = $model->where('pay_count', '>', $where['pay_count']);
        }
        if ($where['user_type'] != '') {
            if ($where['user_type'] == 'routine') $model = $model->where('w.routine_openid', 'not null');
            else if ($where['user_type'] == 'wechat') $model = $model->where('w.openid', 'not null');
            else $model = $model->where('u.user_type', $where['user_type']);
        }
        if ($where['country'] != '') {
            if ($where['country'] == 'domestic') $model = $model->where('w.country', '中国');
            else if ($where['country'] == 'abroad') $model = $model->where('w.country', '<>', '中国');
        }
        return $model;
    }
}